#!/usr/bin/env python
#coding:utf8

import fileutils
import os
import crcutil

SUFFIX_PNG = '.png\n'

# 检查需要额外处理的crc文件后缀
def check_suffix(k):
    return k.endswith('.atlas')

# 修复atlas文件
def fix_crc_atlas(file):
    if not file.endswith('.atlas'):
        return
    if not os.path.isfile(file):
        return
    lines = None
    with open(file, 'r') as f:
        lines = f.readlines()
        pass
    if lines is None or len(lines) == 0:
        return
    with open(file, 'w') as f:
        for line in lines:
            if SUFFIX_PNG in line:
                name = line.replace(SUFFIX_PNG, '')
                crc = crcutil.hex_crc32(name)
                line = crc + SUFFIX_PNG
                print(line)

            f.write(line)
        pass
    pass

# 修复crc处理文件
def fix_crc_files(dir):
    def __walk__(file):
        if check_suffix(file):
            print (file)
            fix_crc_atlas(file)
            pass
        pass
    fileutils.walkfile(dir, __walk__)
    pass